package com.examsystem.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.examsystem.common.ResultJson;
import com.examsystem.common.StatusAndMsg;
import com.examsystem.dao.exame_questionsbankMapper;
import com.examsystem.entity.exame_questionsbank;
import com.examsystem.entity.exame_questionsbankExample;
import com.examsystem.entity.exame_questionsbankExample.Criteria;
import com.examsystem.service.QuesBankService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

@Service
public class QuesBankServiceImpl implements QuesBankService {

	@Autowired
	private exame_questionsbankMapper questionsbankMapper;

	// 查询考题（分页）
	public ResultJson selectQBList(Integer page, Integer pageSize) {
		ResultJson result = null;
		// 操作数据库
		PageHelper.startPage(page, pageSize);
		List<exame_questionsbank> list = questionsbankMapper.selectByExample(null);
		if (null == list || list.size() == 0) {
			// 为空
			result = new ResultJson(StatusAndMsg.NODATA.getStatus(), StatusAndMsg.NODATA.getMsg(), null);
		} else {
			PageInfo<exame_questionsbank> pageInfo = new PageInfo<>(list, pageSize);
			result = new ResultJson(StatusAndMsg.YES.getStatus(), StatusAndMsg.YES.getMsg(), pageInfo);
		}
		return result;
	}

	// 模糊查询(分页）
	public ResultJson selectByExample(exame_questionsbank questionsbank, String Sort, Integer page, Integer pageSize) {
		ResultJson result = null;
		// 构造查询条件
		exame_questionsbankExample example = new exame_questionsbankExample();
		if ("DESC".equals(Sort) || "ASC".equals(Sort)) {
			example.setOrderByClause("createTime" + " " + Sort);
		}
		Criteria criteria = example.createCriteria();
		if (null != questionsbank.getQbid()) {
			// 根据主键查询（唯一）
			criteria.andQbidEqualTo(questionsbank.getQbid());
		} else {
			if (null != questionsbank.getScore()) {
				// 根据分值查询
				criteria.andScoreEqualTo(questionsbank.getScore());
			}
		}
		List<exame_questionsbank> list = questionsbankMapper.selectByExample(example);
		if (null == list || list.size() == 0) {
			// 为空
			result = new ResultJson(StatusAndMsg.NODATA.getStatus(), StatusAndMsg.NODATA.getMsg(), null);
		} else {
			PageInfo<exame_questionsbank> pageInfo = new PageInfo<>(list, pageSize);
			result = new ResultJson(StatusAndMsg.YES.getStatus(), StatusAndMsg.YES.getMsg(), pageInfo);
		}
		return result;
	}

	// 根据id修改
	public ResultJson updateByKey(exame_questionsbank questionsbank) {
		ResultJson result = null;
		// 判断
		if (null == questionsbank.getQbid()) {
			result = new ResultJson(StatusAndMsg.PARAMEERROR.getStatus(), StatusAndMsg.PARAMEERROR.getMsg(), null);
			return result;
		}
		// 操作数据库
		int i = questionsbankMapper.updateByPrimaryKeySelective(questionsbank);
		if (i > 0) {
			result = new ResultJson(StatusAndMsg.YES.getStatus(), StatusAndMsg.YES.getMsg(), null);
		} else {
			result = new ResultJson(StatusAndMsg.SERVICEERROR.getStatus(), StatusAndMsg.SERVICEERROR.getMsg(), null);
		}
		return result;
	}

	// 根据id删除
	public ResultJson deleteByKey(exame_questionsbank questionsbank) {
		ResultJson result = null;
		// 判断
		if (null == questionsbank.getQbid()) {
			result = new ResultJson(StatusAndMsg.PARAMEERROR.getStatus(), StatusAndMsg.PARAMEERROR.getMsg(), null);
			return result;
		}
		int i = questionsbankMapper.deleteByPrimaryKey(questionsbank.getQbid());
		// 操作数据库
		if (i > 0) {
			result = new ResultJson(StatusAndMsg.YES.getStatus(), StatusAndMsg.YES.getMsg(), null);
		} else {
			result = new ResultJson(StatusAndMsg.SERVICEERROR.getStatus(), StatusAndMsg.SERVICEERROR.getMsg(), null);
		}
		return result;
	}

}
